package cn.itcast.user.controller.interceptor;

import cn.itcast.hmall.dto.common.ThreadLocalUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class MyInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String userId = request.getHeader("authorization");
        if(StringUtils.isBlank(userId)){
            return true;
        }
        ThreadLocalUtil.setUserId(Long.valueOf(userId));
        return true;
    }
}
